﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EazyCMS.Common;
using System.Data;

namespace EazyCMS.Web.admin.article
{
    public partial class fenshu_edit :Web.UI.ManagePage
    {
        //string defaultpassword = "0|0|0|0"; //默认显示密码
        protected string action = EazyEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = EazyRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == EazyEnums.ActionEnum.Edit.ToString())
            {
                this.action = EazyEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = EazyRequest.GetQueryInt("id");

            }
            if (!Page.IsPostBack)
            {
                //  ChkAdminLevel("user_list", EazyEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(""); //绑定类别
                if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void TreeBind(string strWhere)
        {
            BLL.jyj_student bll = new BLL.jyj_student();
            DataTable dt = bll.GetList(0, strWhere, "id asc").Tables[0];
            
            this.ddlStudentId.Items.Clear();
            this.ddlStudentId.Items.Add(new ListItem("请选择学生...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlStudentId.Items.Add(new ListItem(dr["user_name"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.jyj_student bll = new BLL.jyj_student();
            Model.student_score model = bll.GetscoreModel(_id);

            //ddlGroupId.SelectedValue = model.group_id.ToString();
            //rblStatus.SelectedValue = model.status.ToString();
            ddlStudentId.SelectedValue = model.User_id.ToString();
            txtDeYu.Text= model.Deyu;
            txtHuaXue.Text = model.Huaxue;
            txtLiShi.Text = model.Lishi;
            txtPingShiChengJi.Text = model.Pingshichengji;
            txtShuXue.Text = model.Shuxue;
            txtWuLi.Text = model.Wuli;
            txtYingYu.Text = model.Yingyu;
            txtYuWen.Text = model.Yuwen;
            txtZhengZhi.Text = model.Zhengzhi;
            txtTiYu.Text = model.Tiyu;
            txtAddTime.Text = model.Add_time.ToShortDateString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.student_score model = new Model.student_score();
            BLL.jyj_student bll = new BLL.jyj_student();
            model.User_id = int.Parse(ddlStudentId.SelectedValue);
            model.Yingyu = txtYingYu.Text.Trim();
            model.Add_time=DateTime.Parse(txtAddTime.Text.Trim());
            model.Shuxue = txtShuXue.Text.Trim();
            model.Yuwen = txtYuWen.Text.Trim();
            model.Huaxue = txtHuaXue.Text.Trim();
            model.Wuli = txtWuLi.Text.Trim();
            model.Lishi = txtLiShi.Text.Trim();
            model.Zhengzhi = txtZhengZhi.Text.Trim();
            model.Deyu = txtDeYu.Text.Trim();
            model.Pingshichengji = txtPingShiChengJi.Text.Trim();
            model.Tiyu = txtTiYu.Text.Trim();
            if (bll.Addscore(model) > 0)
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.jyj_student bll = new BLL.jyj_student();
            Model.student_score model = bll.GetscoreModel(_id);
            model.User_id = int.Parse(ddlStudentId.SelectedValue);
            model.Yingyu = txtYingYu.Text.Trim();
            model.Add_time = DateTime.Parse(txtAddTime.Text.Trim());
            model.Shuxue = txtShuXue.Text.Trim();
            model.Yuwen = txtYuWen.Text.Trim();
            model.Huaxue = txtHuaXue.Text.Trim();
            model.Wuli = txtWuLi.Text.Trim();
            model.Lishi = txtLiShi.Text.Trim();
            model.Zhengzhi = txtZhengZhi.Text.Trim();
            model.Deyu = txtDeYu.Text.Trim();
            model.Tiyu = txtTiYu.Text.Trim();

            model.Pingshichengji = txtPingShiChengJi.Text.Trim();

            if (bll.Updatescore(model))
            {
                //AddAdminLog(EazyEnums.ActionEnum.Edit.ToString(), "修改用户信息:" + model.user_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("student_list.aspx", EazyEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改学生成绩成功！", "fenshu.aspx");
            }
            else //添加
            {
                //ChkAdminLevel("user_list", EazyEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加学生成绩成功！", "fenshu.aspx");
            }
        }

    }
}